<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>

<div class="row">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">发文申请表</h3>
            </div>

            <!-- BASIC FORM ELEMENTS -->
            <!--===================================================-->
            <form id="form_outfile_input" class="panel-body form-horizontal form-padding">
                <input type="hidden" name="keyId" id="keyId" value="${outfile.id}" />

                <div class="form-group">
                    <label class="col-md-2 control-label">发文类别<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <select id="dictItemId" name="dictItemId" class="form-control">
                            <option></option>
                            <c:forEach var="item" items="${dictItems}" varStatus="status">
                                <option value="${item.id}" <c:if test="${item.id == outfile.dictItem.id}"> selected="selected"</c:if>>${item.name}</option>
                            </c:forEach>
                        </select>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">发文标题<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="text" id="name" name="name" class="form-control" placeholder="请输入发文标题" value="${outfile.name}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">文件编号<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input type="text" id="fileNo" name="fileNo" class="form-control" placeholder="请输入文件编号" value="${outfile.fileNo}">
                    </div>
                </div>

                <div class="form-group div-Hidden">
                    <label class="col-md-2 control-label">发文时间<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                            <div class="input-group date">
                                <input type="text" class="form-control" id="datPulished" name="datPulished" placeholder="请选择发文时间"
                                value=<fmt:formatDate pattern="yyyy-MM-dd" value="${outfile.datPulished}" /> >
                                <span class="input-group-addon"><i class="ti-calendar"></i></span>
                            </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">通知对象<i class="fa fa-asterisk txt-color-red"></i>
                        <span id="noticeTypeSpan" style="display: none">${outfile.noticeType+1}</span>
                    </label>
                    <div class="col-md-6">

                        <div class="radio">
                            <!-- Inline radio buttons -->
                            <input type="radio" name="noticeType" id="noticeType1Selected" class="magic-radio" value="0"
                            <c:if test="${outfile.noticeType==0}"> checked="checked"</c:if> >
                            <label for="noticeType1Selected">全体员工</label>

                            <input type="radio" name="noticeType" id="noticeType2Selected" class="magic-radio" value="1"
                            <c:if test="${outfile.noticeType==1}"> checked="checked"</c:if> >
                            <label for="noticeType2Selected">部门</label>

                            <input type="radio" name="noticeType" id="noticeType3Selected" class="magic-radio" value="2"
                            <c:if test="${outfile.noticeType==2}"> checked="checked"</c:if> >
                            <label for="noticeType3Selected">个人</label>

                        </div>
                    </div>
                </div>

                <div class="form-group" id="divNoticeDepts">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_departments_dialog_select('y','deptNames','deptIds')" style="color: #0a6dbd">通知部门</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="deptNames" name="deptNames" class="form-control" placeholder="请选择通知部门"
                               value="<c:forEach var="item" items="${outfile.noticeDepartments}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="deptIds" name="deptIds"
                               value="<c:forEach var="item" items="${outfile.noticeDepartments}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group" id="divNoticeUsers">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_members_dialog_select('userNames','userIds')" style="color: #0a6dbd">通知个人</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="userNames" name="userNames" class="form-control" placeholder="请选择通知个人"
                               value="<c:forEach var="item" items="${outfile.noticeUsers}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="userIds" name="userIds"
                               value="<c:forEach var="item" items="${outfile.noticeUsers}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">
                        <a href="javascript:void(0);" onclick="fn_members_dialog_select('viewerNames','viewerIds')" style="color: #0a6dbd">会签对象</a>
                        <i class="fa fa-asterisk txt-color-red"></i>
                    </label>
                    <div class="col-md-6">
                        <input type="text" id="viewerNames" name="viewerNames" class="form-control" placeholder="请选择会签对象"
                               value="<c:forEach var="item" items="${outfile.viewers}" varStatus="status">${item.name}<c:if test="${!status.last}">,</c:if></c:forEach>">
                        <input type="hidden" id="viewerIds" name="viewerIds"
                               value="<c:forEach var="item" items="${outfile.viewers}" varStatus="status">${item.id}<c:if test="${!status.last}">,</c:if></c:forEach>">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">相关附件<i class="fa fa-asterisk txt-color-red"></i></label>
                    <div class="col-md-6">
                        <input name="uploadify" id="fileAttachment" placeholder="" type="file" >
                        <input name="hiddenFiles" id="hiddenFilesAttachment" type="hidden" value="${outfile.fileAttachment}">
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-md-2 control-label">提交人</label>
                    <div class="col-md-2">
                        <p class="form-control-static">${outfile.creater.name}</p>
                    </div>
                    <label class="col-md-2 control-label">提交日期</label>
                    <div class="col-md-2">
                        <p class="form-control-static"><fmt:formatDate pattern="yyyy-MM-dd" value="${outfile.dateApply}" /></p>
                    </div>
                </div>
            </form>
            <!--===================================================-->
            <!-- END BASIC FORM ELEMENTS -->

            <div class="panel-footer">
                <a class="btn btn-default" id="apply-re-common" href="javascript:void(0)">
                    <i class="fa fa-lg fa-mail-reply"></i> 返回视图
                </a>
                <c:if test="${outfile.numStatus==0 && user.id==outfile.creater.id}">
                    <a class="btn btn-success" id="btn-save-common" href="javascript:void(0);"><i class="fa fa-lg fa-download"></i> 保存表单 </a>
                    <a class="btn btn-warning" id="btn-submit-common" href="javascript:void(0);"><i class="fa fa-upload"></i> 提交表单 </a>
                </c:if>
                <c:if test="${outfile.numStatus==1}">
                    <a class="btn btn-success" id="btn-update-common" href="javascript:void(0);"><i class="fa fa-lg fa-edit"></i> 完成修订 </a>
                </c:if>
            </div>
        </div>
    </div>
</div>

<script>

    // 日期选择
    $('.input-group.date').datepicker({
        format: 'yyyy-mm-dd',
        weekStart: 1,
        autoclose: true,
        todayBtn: 'linked',
        todayHighlight: true,
        language: 'zh-CN'
    });

    // 字典列表下拉框
    $("#dictItemId").select2({
        placeholder: "请选择一个发文类别",
        allowClear: true
    });

    // 附件上传
    inputLoad({
        objId:"fileAttachment",
        entityName:"fileAttachment",
        sourceId:"hiddenFilesAttachment"
    });

    //判断是否已上传了附件
    function hasFile(file,fileUploaded,msg){
        if(fileUploaded.length==0){
            if(file.length==0){
                __toastr_error(msg);
                return false;
            }
        }
        return true;
    }

    function fn_members_dialog_select(nameField,idField) {
        __open_dialog_select('选择成员', '<%=path%>/main/member/dialogs?sids='+$("#userIds").val(), function (dialogRef) {
            var callback = function (data) {
                if (data) {
                    dialogRef.close();

                    // 获取 user
                    $('#'+idField).val(data.ids);
                    $('#'+nameField).val(data.names);
                } else {
                    dialogRef.enableButtons();
                }
            };
            fn_dialog_members_select(callback);
        });
    }

    //校验
    function infoValidate() {
        var tmp = "";

        tmp = $("select#dictItemId").val();
        if(!tmp){
            __toastr_error("请选择发文类别！");
            return false;
        }

        tmp = $("input#name").val();
        if(!tmp){
            __toastr_error("请输入发文标题！");
            return false;
        }

        tmp = $("input#fileNo").val();
        if(!tmp){
            __toastr_error("请输入发文编号！");
            return false;
        }

        tmp = $("input#datPulished").val();
        if(!tmp){
            __toastr_error("请选择发文时间！");
            return false;
        }

        tmp = $("input[name=noticeType]").filter(":checked").val();
        if(tmp=="1"){
            tmp = $("input#deptIds").val();
            if(!tmp){
                __toastr_error("请选择通知部门！");
                return false;
            }
        } else if(tmp=="2"){
            tmp = $("input#userIds").val();
            if(!tmp){
                __toastr_error("请选择通知个人！");
                return false;
            }
        }

        tmp = $("input#viewerNames").val();
        if(!tmp){
            __toastr_error("请选择会签对象！");
            return false;
        }

        file = $("input#hiddenFilesAttachment").val();
        fileUploaded = $("input#fileAttachment").val();
        if(!hasFile(file,fileUploaded,"请上传相关附件！")) return false;

        return true;
    }

    //保存
    $("#btn-save-common").click(
            function(e) {
                __confirm_dialog("提示：","确定保存吗？",
                        function () {
                            __form_save("form_outfile_input", "<%=path%>/activiti/outfile/save");
                            returnToHashUrl();
                        }
                )
            }
    );

    //提交
    $("#btn-submit-common").click(
            function(e) {
                var $valid = infoValidate();
//                console.log("v:"+$valid);
                if(!$valid) return false;

                __confirm_dialog("提示：","提交后填写资料不得修改，请确认是否进行提交？",
                        function () {
                            __form_save("form_outfile_input", "<%=path%>/activiti/outfile/submit");
                            returnToHashUrl();
                        }
                )
            }
    );

    // 完成修改
    $("#btn-update-common").click(
        function(e) {
            var $valid = infoValidate();
            if(!$valid) return false;

            __confirm_dialog("提示：","请确认是否完成修订？",
                function () {
                    __form_save("form_outfile_input", "<%=path%>/activiti/outfile/update");
                    returnToHashUrl();
                }
            )
        }
    );

    //返回视图
    $("#apply-re-common").click(returnToHashUrl);

    // 加载完成后执行
    $(function(){
        $("#noticeType1Selected").click(function(){
            $("#divNoticeDepts").hide();
            $("#divNoticeUsers").hide();
        });

        $("#noticeType2Selected").click(function(){
            $("#divNoticeDepts").show();
            $("#divNoticeUsers").hide();
        });

        $("#noticeType3Selected").click(function(){
            $("#divNoticeDepts").hide();
            $("#divNoticeUsers").show();
        });

        var type = $("#noticeTypeSpan").text();
        $("#noticeType" + type + "Selected").click();
    })

</script>




